<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item> -->
    <el-form-item label="状态" prop="status">
  <el-select v-model="dataForm.status" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="昵称" prop="nikeName">
      <el-input v-model="dataForm.nikeName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="登录账号" prop="loginUser">
      <el-input v-model="dataForm.loginUser" placeholder="登录账号"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="userImage">
      <el-input v-model="dataForm.userImage" placeholder="用户头像"></el-input>

      <oss-upload />
    </el-form-item>
    <el-form-item label="登录密码" prop="loginPass">
      <el-input v-model="dataForm.loginPass" placeholder="登录密码"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="用户地址"></el-input>
    </el-form-item>
    <el-form-item label="邮箱地址" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱地址"></el-input>
    </el-form-item>
    <el-form-item label="单位" prop="company">
      <el-input v-model="dataForm.company" placeholder="单位"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script> import OssUpload from '../oss/oss-upload.vue' 

  export default {
    data () {
      return {
           options: [{
          value: 1,
          label: '正常'
        },
        {
          value: 0,
          label: '冻结'
        }],
      
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          updateTime: '',
          status: '',
          nikeName: '',
          loginUser: '',
          userImage: '',
          loginPass: '',
          phone: '',
          address: '',
          email: '',
          company: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 1正常 0冻结不能为空', trigger: 'blur' }
          ],
          nikeName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          loginUser: [
            { required: true, message: '登录账号不能为空', trigger: 'blur' }
          ],
          userImage: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          loginPass: [
            { required: true, message: '登录密码不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '用户地址不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱地址不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components:{
OssUpload
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/testing/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.user.createTime
                this.dataForm.updateTime = data.user.updateTime
                this.dataForm.status = data.user.status
                this.dataForm.nikeName = data.user.nikeName
                this.dataForm.loginUser = data.user.loginUser
                this.dataForm.userImage = data.user.userImage
                this.dataForm.loginPass = data.user.loginPass
                this.dataForm.phone = data.user.phone
                this.dataForm.address = data.user.address
                this.dataForm.email = data.user.email
                this.dataForm.company = data.user.company
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/testing/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'status': this.dataForm.status,
                'nikeName': this.dataForm.nikeName,
                'loginUser': this.dataForm.loginUser,
                'userImage': this.dataForm.userImage,
                'loginPass': this.dataForm.loginPass,
                'phone': this.dataForm.phone,
                'address': this.dataForm.address,
                'email': this.dataForm.email,
                'company': this.dataForm.company
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
